library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.1.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(readxl)
library(rvest)
## 
## Attaching package: 'rvest'
## The following object is masked from 'package:readr':
## 
##     guess_encoding
library(httr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(flexdashboard)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:httr':
## 
##     config
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

Import dataset

raw_sub_crime = 
  read_csv("./data/subwaycrime.csv") %>% 
  janitor::clean_names()
## New names:
## * `` -> ...1
## Warning: One or more parsing issues, see `problems()` for details
## Rows: 6244 Columns: 37
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (20): BORO_NM, CMPLNT_FR_DT, CMPLNT_TO_DT, CRM_ATPT_CPTD_CD, JURIS_DESC...
## dbl  (11): ...1, CMPLNT_NUM, ADDR_PCT_CD, JURISDICTION_CODE, KY_CD, PD_CD, T...
## lgl   (4): HADEVELOPT, HOUSING_PSA, LOC_OF_OCCUR_DESC, PARKS_NM
## time  (2): CMPLNT_FR_TM, CMPLNT_TO_TM
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
raw_sub_station = 
  read_xlsx("./data/subway_info_final.xlsx") %>% 
  janitor::clean_names()

Tidy data: add time intervals

sub_crime = 
  raw_sub_crime %>% 
  select(cmplnt_fr_dt, cmplnt_fr_tm, ofns_desc, station_name, latitude, longitude) %>% 
  rename("date" = "cmplnt_fr_dt", "time" = "cmplnt_fr_tm", "crime_event" = "ofns_desc") %>% 
  drop_na(time) %>% 
  mutate(
    time = case_when(
      hms("00:00:00") <= time & time < hms("02:00:00") ~hms("00:00:00"),
      hms("02:00:00") <= time & time < hms("04:00:00") ~hms("04:00:00"),
      hms("04:00:00") <= time & time < hms("06:00:00") ~hms("04:00:00"),
      hms("06:00:00") <= time & time < hms("08:00:00") ~hms("08:00:00"),
      hms("08:00:00") <= time & time < hms("10:00:00") ~hms("08:00:00"),
      hms("10:00:00") <= time & time < hms("12:00:00") ~hms("12:00:00"),
      hms("12:00:00") <= time & time < hms("14:00:00") ~hms("12:00:00"),
      hms("14:00:00") <= time & time < hms("16:00:00") ~hms("16:00:00"),
      hms("16:00:00") <= time & time < hms("18:00:00") ~hms("16:00:00"),
      hms("18:00:00") <= time & time < hms("20:00:00") ~hms("20:00:00"),
      hms("20:00:00") <= time & time < hms("23:59:59") ~hms("20:00:00"),
    )
  ) %>% 
  mutate(time = as.character(time))
sub_crime %>% 
  group_by(time, crime_event) %>% 
  summarise(event_num = n())
## `summarise()` has grouped output by 'time'. You can override using the `.groups` argument.
## # A tibble: 147 × 3
## # Groups:   time [6]
##    time  crime_event                    event_num
##    <chr> <chr>                              <int>
##  1 0S    ASSAULT 3 & RELATED OFFENSES          55
##  2 0S    BURGLAR'S TOOLS                        1
##  3 0S    BURGLARY                               1
##  4 0S    CRIMINAL MISCHIEF & RELATED OF       102
##  5 0S    CRIMINAL TRESPASS                      4
##  6 0S    DANGEROUS DRUGS                       24
##  7 0S    DANGEROUS WEAPONS                      9
##  8 0S    FELONY ASSAULT                        34
##  9 0S    FRAUDS                                 1
## 10 0S    GRAND LARCENY                         38
## # … with 137 more rows

Dashboard

bar_plot = 
  sub_crime %>% 
  mutate(time = as.factor(time)) %>% 
  ggplot(aes(x = time, fill = crime_event)) + 
  geom_histogram(stat = "count", width = 0.8) + 
  labs(
    title = "Frequency of crime events v.s. Time points", 
    x = "Occurrence time", 
    y = "Frequency of crime events") + 
  theme_bw() + 
  theme(
    plot.title = element_text(hjust = 1), 
    legend.position = "bottom",
    legend.text = element_text(size = 8)) + 
  guides(col = guide_legend(nrow = 2))
## Warning: Ignoring unknown parameters: binwidth, bins, pad
ggplotly(bar_plot) %>%
  layout(legend = list(
      orientation = "h",
      xanchor = "center",
      yanchor = "top",
      x = 0.5,
      y = - 0.1
    )
  )